package com.pair.micro.common.response;

import com.alibaba.fastjson.JSONObject;
import com.pair.micro.common.constant.MicroBusinessError;
import com.pair.micro.common.constant.MicroSystemError;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

//@ApiModel(value = "MicroResponse对象", description = "响应回参结构")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MicroResponse<T> implements Serializable {
  private static final long serialVersionUID = 1L;

  //// @ApiModelProperty(value = "响应码", required = true, example = "00000")
  private String rtCode;

  // @ApiModelProperty(value = "响应信息", required = true, example = "操作成功")
  private String rtMsg;

  // @ApiModelProperty(value = "请求接口编号", required = true, example = "00000001")
  private String mtCode;

  // @ApiModelProperty(value = "请求接口版本", example = "1.0")
  private String mtVersion;

  // @ApiModelProperty(value = "服务器当前时间戳 毫秒级", required = true, example = "1562901516129")
  private Long ts = System.currentTimeMillis();

  // @ApiModelProperty(value = "返回数据结构")
  private RtData rtData;

  // @ApiModelProperty(value = "操作编码(add/edit/query/delete)", example = "query")
  private String optCode;
  public MicroResponse(String rtCode, String mtCode) {
    this.rtCode = rtCode;
    this.mtCode = mtCode;

    MicroSystemError microSystemError = MicroSystemError.getSysError(rtCode);

    if (null != microSystemError) {
      this.rtMsg = microSystemError.getMsg();
    } else {
      MicroBusinessError microBusinessError = MicroBusinessError.getBisError(rtCode);
      if (null != microBusinessError) {
        this.rtMsg = microBusinessError.getMsg();
      } else {
        this.rtMsg = "未知错误！";
      }
    }
  }

  public MicroResponse(String rtCode, String rtMsg, String mtCode) {
    this.rtCode = rtCode;
    this.rtMsg = rtMsg;
    this.mtCode = mtCode;
    this.rtData = new RtData();
  }

  /**
   * rtData 转换为对应实体
   *
   * @param tClass 类
   * @param <E>    泛型
   * @return Object
   */
  public <E> E getData(Class<E> tClass) {
    if (rtData == null) {
      return null;
    }
    if (tClass.isAssignableFrom(rtData.getClass())) {
      return (E) rtData;
    }
    return JSONObject.parseObject(rtData.toString(), tClass);
  }
  /**
   * 判断请求是否成功
   *
   * @param microResponse
   * @return true成功 false失败
   */
  public static boolean isSuccess(MicroResponse microResponse) {
    if (null != microResponse) {
      if (MicroSystemError.SYS_00000.getCode().equals(microResponse.getRtCode())) {
        return true;
      }
    }

    return false;
  }


  /**
   * 快速构建请求成功对象
   *
   * @param mtCode
   * @return
   */
  public static <T> MicroResponse<T> Success(String mtCode) {
    return new MicroResponse(MicroSystemError.SYS_00000.getCode(), mtCode);
  }

  /**
   * 快速构建请求成功对象
   *
   * @param rtMsg
   * @param mtCode
   * @return
   */
  public static <T> MicroResponse<T> Success(String rtMsg, String mtCode) {
    return new MicroResponse(MicroSystemError.SYS_00000.getCode(), rtMsg, mtCode);
  }

  /**
   * 快速构建请求成功对象
   *
   * @param mtCode
   * @param rtData
   * @return
   */
  public static <T> MicroResponse<T> Success(String mtCode, RtData rtData) {
    MicroResponse microResponse =
        new MicroResponse(MicroSystemError.SYS_00000.getCode(), mtCode);
    microResponse.setRtData(rtData);
    return microResponse;
  }

  /**
   * 快速构建请求成功对象
   *
   * @param rtMsg
   * @param mtCode
   * @param rtData
   * @return
   */
  public static <T> MicroResponse<T> Success(String rtMsg, String mtCode, RtData rtData) {
    MicroResponse microResponse =
        new MicroResponse(MicroSystemError.SYS_00000.getCode(), rtMsg, mtCode);
    microResponse.setRtData(rtData);
    return microResponse;
  }
  /**
   * 快速构建请求成功对象 例如传boolean
   *
   * @param rtData
   * @return
   */
  public static <T> MicroResponse<T> Success(RtData rtData) {
    MicroResponse<T> result = new MicroResponse<>();
    result.setRtData(rtData);
    return result;
  }

  /**
   * 快速构建请求失败对象
   *
   * @param mtCode
   * @return
   */
  public static <T> MicroResponse<T> Fail(String mtCode) {
    return new MicroResponse(MicroSystemError.SYS_00001.getCode(), mtCode);
  }

  /**
   * 快速构建请求失败对象
   *
   * @param mtCode
   * @param rtData
   * @return
   */
  public static <T> MicroResponse<T> Fail(String mtCode, RtData rtData) {
    MicroResponse microResponse =
        new MicroResponse(MicroSystemError.SYS_00001.getCode(), mtCode);
    microResponse.setRtData(rtData);
    return microResponse;
  }

  /**
   * 快速构建请求失败对象
   *
   * @param rtMsg
   * @param mtCode
   * @return
   */
  public static <T> MicroResponse<T> Fail(String rtMsg, String mtCode) {
    return new MicroResponse(MicroSystemError.SYS_00001.getCode(), rtMsg, mtCode);
  }

  /**
   * 快速构建请求失败对象
   *
   * @param rtMsg
   * @param mtCode
   * @param rtData
   * @return
   */
  public static <T> MicroResponse<T> Fail(String rtMsg, String mtCode, RtData rtData) {
    MicroResponse microResponse =
        new MicroResponse(MicroSystemError.SYS_00001.getCode(), rtMsg, mtCode);
    microResponse.setRtData(rtData);
    return microResponse;
  }

  /**
   * 快速构建业务异常对象
   *
   * @param microBusinessError
   * @param mtCode
   * @return
   */
  public static <T> MicroResponse<T> BisError(MicroBusinessError microBusinessError, String mtCode) {
    return new MicroResponse(
        microBusinessError.getCode(), microBusinessError.getMsg(), mtCode);
  }

  /**
   * 快速构建业务异常对象
   *
   * @param microBusinessError
   * @param microBusinessErrorMsg
   * @param mtCode
   * @return
   */
  public static <T> MicroResponse<T> BisError(
          MicroBusinessError microBusinessError, String microBusinessErrorMsg, String mtCode) {
    return new MicroResponse(microBusinessError.getCode(), microBusinessErrorMsg, mtCode);
  }

  /**
   * 快速构建系统异常对象
   *
   * @param microSystemError
   * @param mtCode
   * @return
   */
  public static <T> MicroResponse<T> SysError(MicroSystemError microSystemError, String mtCode) {
    return new MicroResponse(microSystemError.getCode(), microSystemError.getMsg(), mtCode);
  }

  /**
   * 快速构建系统异常对象
   *
   * @param microSystemError
   * @param microSystemErrorMsg
   * @param mtCode
   * @return
   */
  public static <T> MicroResponse<T> SysError(
          MicroSystemError microSystemError, String microSystemErrorMsg, String mtCode) {
    return new MicroResponse(microSystemError.getCode(), microSystemErrorMsg, mtCode);
  }

}
